import base64_encode from "../../../utils/base64_encode";
import { Toast } from 'vant';
import cTitle from "components/title";


export default {
  data() {
    return {
      bank_name: "",
      bank_id: "",
      bank_user: "",
      bank_province: "",
      bank_city: "",
      bank_branch: "",
      result: '1',
      // status: false,
      bankInfo: {},//银行卡信息
      bankMobile: "",
      bankIdcard: "",

      mch_order_no:"",

      code_active: false,
      code_val: "",
      phone: "",

      timer: "",
      count: "",
      btnText: "发送验证码",
      btnTag: false,
      btnBg: "#ef4f4f"
    };
  },

  mounted() {
    // console.log(this.$route.params);
    // this.phone = this.$route.params.num;
    // if (!this.$route.params.num) {
    //   this.$router.go(-1)
    // }
  },

  methods: {

    //发送验证码
    verificationCode() {
      let that = this;
      if (this.fun.isTextEmpty(this.bankMobile)) {
        Toast("请输入银行卡预留手机号");
        return;
      }
      if (this.fun.isMoblie(this.bankMobile)) {
        Toast("请填写正确的手机号");
        return;
      } 
      if (this.fun.isTextEmpty(this.bank_id)) {
        Toast({
          message: "请输入银行卡号",
          timeout: 1500
        });
        return;
      }

      if (this.fun.isTextEmpty(this.bank_user)) {
        Toast({
          message: "请输入姓名",
          timeout: 1500
        });
        return;
      }
      if (this.fun.isTextEmpty(this.bankIdcard) && this.bankInfo.hasOwnProperty('idcard')) {
        Toast({
          message: "请输入身份证",
          timeout: 1500
        });
        return;
      }


      let json = {
        "payername":this.bank_user,
        "bankcardno":this.bank_id,
        "idno" : this.bankIdcard,
        "mobileno" : this.bankMobile
      };
      
      
        Toast.loading({
          duration: 0, // 持续展示 toast
          forbidClick: true,
          message: "发送中"
        });
        //发送获取验证码的请求
        $http
          .get("member.member-fastpay.signsms",json )
          .then(
            response => {
              Toast.clear();
              if (response.result == 1) {
                that.mch_order_no = response.data.mch_order_no;
                that.sendCodeAnimation();
              }else{
                Toast(response.msg)
              }
            },
            response => {
              Toast(response.msg)
              console.log(response.msg + "nihao");
            }
          );
      
    },
    //发送验证码 倒计时
    sendCodeAnimation() {
      const TIME_COUNT = 60;
      if (!this.timer) {
        this.count = TIME_COUNT;
        Toast.success("验证码已发送");
        this.timer = setInterval(() => {
          if (this.count > 0 && this.count <= TIME_COUNT) {
            this.count--;
            this.btnText = this.count + "s后重新获取";
            this.btnTag = true;
            this.btnBg = "#999";
          } else {
            this.btnTag = false;
            this.btnBg = "#ef4f4f";
            this.btnText = "发送验证码";
            clearInterval(this.timer);
            this.timer = null;
          }
        }, 1000);
      }
    },
    change() {
      // this.code_active = true
      // this.status = true;
      // this.bank_id = "";
    },

    init() {
      this.bank_name = "";
      this.bank_id = "";
      this.bank_user = "";
      this.bank_province = "";
      this.bank_city = "";
      this.bank_branch = "";
    },


    //绑卡
    bind() {
      let that = this;
      
      
      $http.post("member.member-fastpay.smssign", {mchorderno:that.mch_order_no,smscode:that.code_val}, "绑定中...").then((response) => {

        if (response.result == 1) {
          if (window.history.length <= 1) {
            that.$router.push(that.fun.getUrl('home', {}));
          } else {
            that.$router.go(-1);
          }
        } else {
          that.$dialog.alert({ message: response.msg });
        }
      }, (response) => {
        that.$dialog.alert({ message: response.msg });
      });
    }

  },
  activated() {
    this.init();
  },
  watch: {
    bank_name(v) {
      let newVal = v.replace(/[0-9]+/g, '')
      this.bank_name = newVal.replace(/\s*/g, '');
    }
  },

  components: { cTitle }


};
